package isv

import (
    "strings"
    "dhfshop/models/isv"
	"dhfshop/controllers/base"
	//"github.com/astaxie/beego"
	"github.com/astaxie/beego/validation"
	"wxsdk/miniapp/wxapp"
)

type IsvDomainController struct {
	base.BaseController
}

// 修改服务器地址
func (this *IsvDomainController) DomainUpdate() {
    v := struct {
	    AuthorizerAppid  string  `form:"authorizer_appid" valid:"Required"`
		Action           string  `form:"action" valid:"Required"`
		RequestDomain    string  `form:"requestdomain" valid:"Required"`
		WsRequestDomain  string  `form:"wsrequestdomain" valid:"Required"`
		UploadDomain     string  `form:"uploaddomain" valid:"Required"`	
		DownloadDomain   string  `form:"downloaddomain" valid:"Required"`	
	}{} 
	this.ParseForm(&v)
	valid := validation.Validation{}
	passed, _ := valid.Valid(&v)
	if !passed {
		this.ServeError(valid.Errors[0].Key+" "+valid.Errors[0].Message)
		return 
	}
	custAccount, err := isv.GetCustAccountByAppId(v.AuthorizerAppid)
	if err != nil {
	    this.ServeError(err.Error())
		return
	}
	data, code, err := wxapp.ModifyDomain(custAccount.AccessToken,v.Action,
	                                       strings.Split(v.RequestDomain,","),strings.Split(v.WsRequestDomain,","),
										   strings.Split(v.UploadDomain,","),strings.Split(v.DownloadDomain,","))
	if err != nil {
	    this.ServeError(err.Error())
		return
	}
	rsp := struct {
		Code   int     `json:"code"`
	    Data   string  `json:"data"`
	}{Code:code, Data:data}
	this.Data["json"] = rsp
	this.ServeJSON()
}
